#lang racket

(define stack 
  '()); for numeric values only.

(define (push item)
  (if (number? item)
      ( set! stack (cons item stack))
   ; else
      (error "You have attempted to push a non-numeric value onto the stack. The stack only accepts numbers.")  
  )
  (display stack))

(define poppedValue 0)

(define (pop)
  (if (null? stack)
      (error "You have attempted to pop an empty stack. The stack does not permit that operation.")
  ; else
      (set! poppedValue (car stack)))
      (set! stack (cdr stack)) poppedValue)

(define (add) 
  (push (+ (pop) (pop) )))

(define (mul) 
  (push (* (pop) (pop) )))

(define (sub)
  (push (- (pop) (pop) )))

(define (div)
  (push (/ (pop) (pop) )))

(define (inv)
  (if (or (null? stack)  (= 0 (car stack)  ))
  (error "You have attempted to take the inverse of an empty stack or of 0. The stack does not permit those operations.")
  ; else 
      (push (/ 1 (pop) ))))

(define (sgn)
  (if (null? stack)
  (error "You have attempted to change the sign of an empty stack. The stack does not permit that operation")
  (push (* -1 (pop) ))))

(define (exp)
  (push (expt (pop) (pop) )))

(define (clx) 
  (set! stack '()) 
  (display stack) )

(define (p) 
  (display stack))

; Functions from HP-41CV manual for use in testing code
(define (HEAT) 
   (push 30) (push .47) (mul) (mul) (display "...Finished calculating\n\n") (display stack) )

  

